<template>
  <div class="container1" :style="{ backgroundImage: 'url(' + require('@/assets/bg1.jpg') + ')' }">
    <div class="title">
      <span class="logo">
        <div class="logo-img">
          <img src="../assets/logo1.png" alt="" />
        </div>
        <span class="logotitle">乡村规划编制管理系统</span>
      </span>
      <div class="t-left">
        <img src="@/assets/shezhi.png" alt="" /><img src="@/assets/zhuyi.png" alt="" @click="openversion()" />
      </div>
    </div>
    <div class="login">
      <div class="formLeft">
        <p>欢迎登录</p>
        <p>乡村规划编制管理系统</p>
      </div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" label-position="left" label-width="0px" class="login-form">
        <div class="label">用户名</div>
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" type="text" class="einput" auto-complete="off" placeholder="账号">
            <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <div class="label">密码</div>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" class="einput" type="password" auto-complete="off" placeholder="密码" show-password @keyup.enter.native="handleLogin">
            <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <!-- <el-form-item prop="code">
        <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 63%" @keyup.enter.native="handleLogin">
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode">
        </div>
      </el-form-item> -->
        <div class="guize">
          <el-checkbox v-model="loginForm.rememberMe" class="echeck">
            记住我
          </el-checkbox>
          <!-- <span class="dpwd">忘记密码？</span> -->
        </div>
        <div class="denglu">
          <el-button :loading="loading" size="medium" type="primary" style="width: 100%" @click.native.prevent="handleLogin">
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
        </div>
        <div class="zhuce">
          <span>没有账号？</span><span class="alink" @click="toZhuce">注册</span>
        </div>
      </el-form>
      <!--  底部  -->
      <!-- <div v-if="$store.state.settings.showFooter" id="el-login-footer">
      <span v-html="$store.state.settings.footerTxt" />
      <span> ⋅ </span>
      <a href="https://beian.miit.gov.cn/#/Integrated/index" target="_blank">{{ $store.state.settings.caseNumber }}</a>
    </div> -->
    <div></div>
    </div>
  </div>
</template>

<script>
import { encrypt } from "@/utils/rsaEncrypt";
import Config from "@/settings";
import seting from "../../package.json";
import Cookies from "js-cookie";
export default {
  name: "Login",
  data () {
    return {
      activeName: "second", // 选中角色
      codeUrl: "",
      cookiePass: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "用户名不能为空" },
        ],
        password: [
          { required: true, trigger: "blur", message: "密码不能为空" },
        ],
        // code: [
        //   { required: true, trigger: "change", message: "验证码不能为空" },
        // ],
      },
      loading: false,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created () {
    // 获取验证码
    // this.getCode()
    // 获取用户名密码等Cookie
    this.getCookie();
    // token 过期提示
    this.point();
  },
  methods: {
    handleClick (tab, event) {
    },
    openversion(){
      alert('当前前端编译版本：'+seting.version);
    },
    toZhuce () {
      this.$router.push("/register");
    },
    getCode () { },
    getCookie () {
      const username = Cookies.get("username");
      let password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      // 保存cookie里面的加密后的密码
      this.cookiePass = password === undefined ? "" : password;
      password = password === undefined ? this.loginForm.password : password;
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password,
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
        code: "",
      };
    },
    handleLogin () {
      this.$refs.loginForm.validate((valid) => {
        const user = {
          username: this.loginForm.username,
          password: this.loginForm.password,
          rememberMe: this.loginForm.rememberMe,
          code: this.loginForm.code,
          uuid: this.loginForm.uuid,
        };
        if (user.password !== this.cookiePass) {
          user.password = encrypt(user.password);
        }
        if (valid) {
          this.loading = true;
          if (user.rememberMe) {
            Cookies.set("username", user.username, {
              expires: Config.passCookieExpires,
            });
            Cookies.set("password", user.password, {
              expires: Config.passCookieExpires,
            });
            Cookies.set("rememberMe", user.rememberMe, {
              expires: Config.passCookieExpires,
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          this.$store
            .dispatch("Login", user)
            .then(() => {
              this.loading = false;
              if (this.$store.getters.user.roles[0].name == "编制单位用户") {
                this.$router.push("/home2/index");
              } else {
                this.$router.push("/index");
              }
            })
            .catch((e) => {
              this.loading = false;
              this.$notify({
                title:"登录失败",
                message: e.response.data.message,
              })
            });
        } else {
          return false;
        }
      });
    },
    point () {
      const point = Cookies.get("point") !== undefined;
      if (point) {
        this.$notify({
          title: "提示",
          message: "当前登录状态已过期，请重新登录！",
          type: "warning",
          duration: 5000,
        });
        Cookies.remove("point");
      }
    },
  },
};
</script>

<style  lang="scss" scoped>

.container1 {
  position: relative;
  width: 100%;
  background-size: cover;
  height: 100%;
}
.title {
  display: flex;
  justify-content: space-between;
  background-color: #4778c7;
  color: #fff;
  line-height: 80px;
  align-content: center;
  .logo {
    display: flex;
    align-items: center;
    width: 350px;
    height: 65px;
    margin: 0 10px;
    .logo-img {
      width: 50px;
      height: 50px;

      border-radius: 50%;
      text-align: center;
      margin-right: 10px;
      vertical-align: middle;
      img {
        width: 50px;
        height: 50px;
      }
    }
    .logotitle {
      height: 50px;
      line-height: 50px;
      font-family: Arial, Helvetica, sans-serif;
      color: #fff;
      font-size: 24px;
      letter-spacing: 1px;
      font-weight: bold;
    }
  }
  .t-left {
    display: flex;
    align-items: center;
  }

  .wenzi {
    color: #fff;
    font-size: 28px;
    letter-spacing: 1px;
    font-weight: 700;
  }
  img {
    width: 30px;
    height: 30px;
    margin-right: 10px;
  }
}
.login {
  display: flex;
  width: 766px;
  height:450px;
  background-size: cover;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.formLeft {
  width: 50%;
  background-color: rgba(139, 139, 137, 0.5);
  color: #325890;
  font-size: 30px;
  font-weight: 700;
  border-top-left-radius: 6px;
  border-bottom-left-radius: 6px;
  padding-top: 10%;
  padding-left: 10%;
  p {
    font-size: 30px;
    font-weight: 700;
    color: #4778c7;
    margin-bottom: 30px;
  }
}

.title {
  margin: 0 auto 30px auto;
  text-align: center;
  color: #707070;
}
.guize {
  font-size: 16px;
  color: #606266;
  align-items: center;
  width: 100%;
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
  .echeck span {
    font-size: 16px;
  }
  .dpwd {
    width: 100px;
    color: #4778c7;
    &:hover {
      color: red;
    }
  }
}

.zhuce {
  margin-top: 35px;
  display: flex;
  justify-content: center;
  span {
    font-size: 18px;
  }
  .alink {
    color: #4778c7;
    &:hover {
      color: red;
    }
  }
}

::v-deep .el-input {
  display: flex;
  align-items: center;
  height: 52px !important;
  .el-input__inner {
    height: 52px !important;
    line-height: 52px;
    background-color: rgba(205, 201, 201, 0.7);
  }
}
.denglu {
  margin-top: 20px;
  display: flex;
  width: 180px;
  height: 45px;
  margin-left: 50%;
  transform: translateX(-50%);
  button {
    line-height: 25px;
    font-size: 18px;
    font-weight: 500;
    letter-spacing: 10px;
    text-align: center;
    background-color: #497ac7;
  }
}
.login-form {
  border-top-right-radius: 6px;
  border-bottom-right-radius: 6px;
  padding: 55px !important;
  border: 1px solid #ccc;
  .label {
    font-size: 18px;
    margin-bottom: 10px;
    letter-spacing: 1px;
  }
  background: rgba(255, 255, 255, 0.5);
  width: 50%;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 52px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  display: inline-block;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
</style>
